package com.wt.springsamples.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.sun.jndi.toolkit.url.Uri;
import com.wt.springsamples.biz.service.IOrderService;
import com.wt.springsamples.common.ApiResult;
import com.wt.springsamples.config.HttpClientTemplate;
import com.wt.springsamples.model.OrderEntity;
import com.wt.springsamples.model.SysUser;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.net.URI;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/test")
public class TestRedisController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private HttpClientTemplate httpClientTemplate;


//    @Autowired
//    private RedisApi redisApi;

//    @GetMapping("/getLock/{key}")
//    public ApiResult getLock(@PathVariable String key){
//        return ApiResult.ok( redisApi.getLock(key));
//    }
//
    @GetMapping("/cc")
    public ApiResult<?> getById(){
        return ApiResult.ok(new SysUser());
    }

    //http://localhost:8080/api/test/paramsList?ids=1,2
    @GetMapping("/paramsList")
    public ApiResult<?> paramsList(@RequestParam("taskIds") List<Integer> paramsList,HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        return  ApiResult.ok(cookies);
    }
    @PostMapping("/postUser")
    public ApiResult<?> postUser(@RequestBody SysUser sysUser,HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        Enumeration<String> headerNames = request.getHeaderNames();
        HttpSession session = request.getSession();
        while (headerNames.hasMoreElements()){
            System.out.println(request.getHeader(headerNames.nextElement()));
        }
        System.out.println("cookies="+cookies);
        System.out.println("session="+session);
        return ApiResult.ok(sysUser);
    }

    @GetMapping("/testRestCallPostUser")
    public ApiResult<?> testCallPostUser(HttpServletRequest request){
        Cookie[] cookies = request.getCookies();
        HttpHeaders headers = new HttpHeaders();
        headers.set("app-name", "desk");
        headers.set("x-site", "0");
        headers.setContentType(MediaType.APPLICATION_JSON);

        JSONObject params = new JSONObject();
        params.put("id",1000);
        params.put("name","wangtao");
        HttpEntity<String> httpEntity = new HttpEntity<>(params.toJSONString(),headers);
        String url="http://localhost:8080/api/test/postUser";
        ResponseEntity<String> response = restTemplate.exchange(url,HttpMethod.POST,httpEntity,String.class);
        String result = response.getBody();
        return  ApiResult.ok(result);
    }

    @GetMapping("/testHttpCallPostUser")
    public ApiResult<?> testCallPostUser2(HttpServletRequest request){
        Map<String,String> headersMap = new HashMap<>();
        headersMap.put("app-name", "desk");
        headersMap.put("x-site", "0");

        JSONObject params = new JSONObject();
        params.put("id",1000);
        params.put("name","wangtao");
        String url="http://localhost:8080/api/test/postUser";
        String result = httpClientTemplate.doPostJson(url,params.toJSONString(),headersMap);
        return  ApiResult.ok(result);
    }

    @GetMapping("/getXJob")
    public ApiResult<?> getXJob(){
        String xAuthToken = "ZDc3ZTlkNGQtNzhkNy00NGE1LWJjZGMtMWMwNGI0ODA0YTZi";
        String xRenderJobId = "195686";
        Map<String,String> headersMap = new HashMap<>();
        headersMap.put("appname", "desk");
        headersMap.put("x-site", "0");
        headersMap.put("x-auth-token", xAuthToken);
        String url="https://test-gw.xrender.com/renderhippo/groups/"+xRenderJobId;
        String body = httpClientTemplate.doGet(url,headersMap);
        return ApiResult.ok(body);
    }

    @GetMapping("/getXTaskList")
    public ApiResult<?> getXTaskList(){
        String xAuthToken = "Mzk5YTc4OGItZWQwZS00M2IzLTk1M2EtZWMyZDA5OGVlMzY5";
        String xRenderJobId = "195686";
        Map<String,String> headersMap = new HashMap<>();
        headersMap.put("appname", "desk");
        headersMap.put("x-site", "0");
        headersMap.put("x-auth-token", xAuthToken);
        String url="https://test-gw.cudatec.com/renderhippo/c_missions/super";
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
//        builder.queryParam("taskIds", Lists.newArrayList("cbd1674f3bbe40d0ad26665b4938dd8f"));
        builder.queryParam("pageNumber", 1);
        builder.queryParam("pageSize", 10);
        builder.queryParam("history", 0);
        String body = httpClientTemplate.doGet(builder.toUriString(),headersMap);
        return ApiResult.ok(body);
    }


    @GetMapping("/getTaskList")
    public ApiResult<?> getTaskList(){
        String xAuthToken = "YTJmNjA0NWYtNzU0NS00MmRkLWE5NWItZjNiYmI5MmQ4ZmFj";
        String xRenderJobId = "195538";
        HttpHeaders headers = new HttpHeaders();
        headers.set("appname", "desk");
        headers.set("x-auth-token", xAuthToken);
        headers.set("X-SITE", "0");
        HttpEntity<String> httpEntity = new HttpEntity<>(headers);
        String url="https://test-gw.cudatec.com/renderhippo/c_missions/super";
//        String url="http://localhost:8080/api/test/paramsList";
        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url);
        builder.queryParam("taskIds", Lists.newArrayList("cbd1674f3bbe40d0ad26665b4938dd8f"));
//        builder.queryParam("xAuthToken", xAuthToken);
        builder.queryParam("pageNumber", 1);
        builder.queryParam("pageSize", 10);
        builder.queryParam("history", 0);
//        builder.queryParam("searchType", 0);
//        builder.queryParam("keywords", 776738);
//        String[] ids = {"1", "2", "3"};
//        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://example.com/api/data/{ids}");
//        for (int i = 0; i < ids.length; i++) {
//            builder.queryParam("ids", ids[i]); // 对于每个ID添加一个查询参数
//        }
//        URI uri = builder.build(false).toUri();// false 表示不编码URL，因为每个id已经被单独编码了
//        //注意这里用的是 exchange 而非 getForEntity，因为需要自定义URI构建方式
//        ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, null, String.class);
        ResponseEntity<String> response = restTemplate.exchange(builder.toUriString(),HttpMethod.GET,httpEntity,String.class);
        String body = response.getBody();
        return ApiResult.ok(body);
    }

    @GetMapping("/fullRender")
    public ApiResult<?> fullRender(){
        String xAuthToken = "YTJmNjA0NWYtNzU0NS00MmRkLWE5NWItZjNiYmI5MmQ4ZmFj";
        String xRenderJobId = "195538";
        HttpHeaders headers = new HttpHeaders();
        headers.set("appname", "desk");
        headers.set("x-auth-token", xAuthToken);
        headers.set("X-SITE", "0");
        headers.setContentType(MediaType.APPLICATION_JSON);

        JSONObject params = new JSONObject();
        params.put("ids",Lists.newArrayList(xRenderJobId));
        HttpEntity<String> httpEntity = new HttpEntity<>(params.toJSONString(),headers);
        String url="https://test-gw.cudatec.com/renderhippo/c_missions/super/full";
//        String result = restTemplate.postForObject(url,httpEntity,String.class);
        ResponseEntity<String> response = restTemplate.exchange(url,HttpMethod.PUT,httpEntity,String.class);
        String result = response.getBody();
        return ApiResult.ok(result);
    }

}
